package org.vordel.incubator.examples.management_api;

import java.io.File;

import com.vordel.es.fed.FederatedEntityStore;
import com.vordel.pd.agent.ManagementAgentUtil;
import com.vordel.pd.generated.agent.management.ManagementAgent;

public class RemoteSession extends Session {

	protected File workingDriectory;
	private ManagementAgent agent;
	

    protected RemoteSession(String host, int port, String adminname, String password, File workingDir) throws Exception {
		super();
		this.workingDriectory = workingDir;
		this.agent = ManagementAgentUtil.getManagementAgent("http://"+host+":"+port+"/runtime/management/ManagementAgent", adminname, password);
		this.downloadConfiguration();
		this.initSession();
	}
    
    private void downloadConfiguration() throws Exception {
		
		 
	     this.entityStore = ManagementAgentUtil.getActiveConfigurationFedStore(this.agent, this.workingDriectory);
	       
	}
    
    public void commit() throws Exception {
    	super.commit();
    	this.uploadConfiguration();
    }
    
    private boolean uploadConfiguration() throws Exception {
		
		  com.vordel.pd.generated.DeploymentResult result = ManagementAgentUtil.deploy(this.agent, this.workingDriectory);
	        
	        System.out.println(result.isStatus());
	        System.out.println(result.getFailureReason());
	      
	     return result.isStatus();
		
	}

}
